home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Sample.bin / TablePanel.java < prev    next >
Text File  |  1998-06-30  |  17KB  |  420 lines

  1. /*
  2.  * @(#)TablePanel.java    1.25 98/01/31
  3.  *
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  *
  19.  */
  20.  
  21. import com.sun.java.swing.*;
  22. import com.sun.java.swing.table.*;
  23. import com.sun.java.swing.event.*;
  24. import com.sun.java.swing.border.*;
  25.  
  26. import java.awt.*;
  27. import java.awt.event.*;
  28. import java.util.*;
  29.  
  30. /*
  31.  * @version 1.25 01/31/98
  32.  * @author Philip Milne
  33.  * @author Steve Wilson
  34.  */
  35. public class TablePanel extends JPanel {
  36.     JTable      tableView;
  37.     JScrollPane scrollpane;
  38.     Dimension   origin = new Dimension(0, 0);
  39.  
  40.     JCheckBox   isColumnReorderingAllowedCheckBox;
  41.     JCheckBox   showHorizontalLinesCheckBox;
  42.     JCheckBox   showVerticalLinesCheckBox;
  43.  
  44.     JCheckBox   isColumnSelectionAllowedCheckBox;
  45.     JCheckBox   isRowSelectionAllowedCheckBox;
  46.     JCheckBox   isRowAndColumnSelectionAllowedCheckBox;
  47.  
  48.     JLabel      interCellSpacingLabel;
  49.     JLabel      rowHeightLabel;
  50.  
  51.     JSlider     interCellSpacingSlider;
  52.     JSlider     rowHeightSlider;
  53.  
  54.     JComponent  selectionModeButtons;
  55.     JComponent  resizeModeButtons;
  56.  
  57.     JPanel      mainPanel;
  58.     JPanel      controlPanel;
  59.     JScrollPane tableAggregate;
  60.  
  61.     public TablePanel(SwingSet swing) {
  62.     super();
  63.  
  64.     setLayout(new BorderLayout());
  65.         mainPanel = this;
  66.     controlPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
  67.     JPanel column1 = new JPanel (new ColumnLayout() );
  68.     JPanel column2 = new JPanel (new ColumnLayout() );
  69.     JPanel column3 = new JPanel (new ColumnLayout() );
  70.     JPanel column4 = new JPanel (new ColumnLayout() );
  71.  
  72.     mainPanel.add(controlPanel, BorderLayout.NORTH);
  73.  
  74.  
  75.     // start column 1
  76.         isColumnReorderingAllowedCheckBox = new JCheckBox("Reordering allowed", true);
  77.         column1.add(isColumnReorderingAllowedCheckBox);
  78.         isColumnReorderingAllowedCheckBox.addActionListener(new ActionListener() {
  79.         public void actionPerformed(ActionEvent e) {
  80.             boolean flag = ((JCheckBox)e.getSource()).isSelected();
  81.                 tableView.getTableHeader().setReorderingAllowed(flag);
  82.                 tableView.repaint();
  83.         }
  84.         });
  85.  
  86.  
  87.         showHorizontalLinesCheckBox = new JCheckBox("Horiz. Lines", true);
  88.         column1.add(showHorizontalLinesCheckBox);
  89.         showHorizontalLinesCheckBox.addActionListener(new ActionListener() {
  90.         public void actionPerformed(ActionEvent e) {
  91.             boolean flag = ((JCheckBox)e.getSource()).isSelected();
  92.                 tableView.setShowHorizontalLines(flag); ;
  93.                 tableView.repaint();
  94.         }
  95.         });
  96.  
  97.         showVerticalLinesCheckBox = new JCheckBox("Vert. Lines", true);
  98.         column1.add(showVerticalLinesCheckBox);
  99.         showVerticalLinesCheckBox.addActionListener(new ActionListener() {
  100.         public void actionPerformed(ActionEvent e) {
  101.             boolean flag = ((JCheckBox)e.getSource()).isSelected();
  102.                 tableView.setShowVerticalLines(flag); ;
  103.                 tableView.repaint();
  104.         }
  105.         });
  106.  
  107.         interCellSpacingLabel = new JLabel("Inter-cell spacing:");
  108.     column1.add(interCellSpacingLabel);
  109.  
  110.         interCellSpacingSlider = new JSlider(JSlider.HORIZONTAL, 0, 10, 1);
  111.     interCellSpacingSlider.getAccessibleContext().setAccessibleName("Inter-cell spacing");
  112.     interCellSpacingLabel.setLabelFor(interCellSpacingSlider);
  113.         column1.add(interCellSpacingSlider);
  114.         interCellSpacingSlider.addChangeListener(new ChangeListener() {
  115.         public void stateChanged(ChangeEvent e) {
  116.             int spacing = ((JSlider)e.getSource()).getValue();
  117.                 tableView.setIntercellSpacing(new Dimension(spacing, spacing));
  118.                 tableView.repaint();
  119.         }
  120.         });
  121.  
  122.         controlPanel.add(column1);
  123.  
  124.     // start column 2
  125.  
  126.      isColumnSelectionAllowedCheckBox = new JCheckBox("Column selection", false);
  127.         column2.add(isColumnSelectionAllowedCheckBox);
  128.         isColumnSelectionAllowedCheckBox.addActionListener(new ActionListener() {
  129.         public void actionPerformed(ActionEvent e) {
  130.             boolean flag = ((JCheckBox)e.getSource()).isSelected();
  131.                 tableView.setColumnSelectionAllowed(flag); ;
  132.                 tableView.repaint();
  133.         }
  134.         });
  135.  
  136.         isRowSelectionAllowedCheckBox = new JCheckBox("Row selection", true);
  137.         column2.add(isRowSelectionAllowedCheckBox);
  138.         isRowSelectionAllowedCheckBox.addActionListener(new ActionListener() {
  139.         public void actionPerformed(ActionEvent e) {
  140.             boolean flag = ((JCheckBox)e.getSource()).isSelected();
  141.                 tableView.setRowSelectionAllowed(flag); ;
  142.                 tableView.repaint();
  143.         }
  144.         });
  145.  
  146.         isRowAndColumnSelectionAllowedCheckBox = new JCheckBox("Cell selection", false);
  147.         column2.add(isRowAndColumnSelectionAllowedCheckBox);
  148.         isRowAndColumnSelectionAllowedCheckBox.addActionListener(new ActionListener() {
  149.         public void actionPerformed(ActionEvent e) {
  150.             boolean flag = ((JCheckBox)e.getSource()).isSelected();
  151.                 tableView.setCellSelectionEnabled(flag); ;
  152.                 tableView.repaint();
  153.         }
  154.         });
  155.  
  156.         rowHeightLabel = new JLabel("Row height:");
  157.     column2.add(rowHeightLabel);
  158.  
  159.         rowHeightSlider = new JSlider(JSlider.HORIZONTAL, 5, 100, 20);
  160.     rowHeightSlider.getAccessibleContext().setAccessibleName("Row height");
  161.     rowHeightLabel.setLabelFor(rowHeightSlider);
  162.         column2.add(rowHeightSlider);
  163.         rowHeightSlider.addChangeListener(new ChangeListener() {
  164.         public void stateChanged(ChangeEvent e) {
  165.             int height = ((JSlider)e.getSource()).getValue();
  166.                 tableView.setRowHeight(height);
  167.                 tableView.repaint();
  168.         }
  169.         });
  170.  
  171.         controlPanel.add(column2);
  172.  
  173.         // Radio buttons for selection modes.
  174.     column3.setBorder(new TitledBorder("Selection"));
  175.         column3.setLayout(new ColumnLayout());
  176.  
  177.     ButtonGroup selectionModeGroup = new ButtonGroup();
  178.  
  179.     final JRadioButton selectionModeButton1 = new JRadioButton("Single", false);
  180.     selectionModeGroup.add(selectionModeButton1);
  181.     column3.add(selectionModeButton1);
  182.  
  183.     final JRadioButton selectionModeButton2 = new JRadioButton("One range", false);
  184.     selectionModeGroup.add(selectionModeButton2);
  185.     column3.add(selectionModeButton2);
  186.  
  187.     final JRadioButton selectionModeButton3 = new JRadioButton("Multiple ranges", true);
  188.     selectionModeGroup.add(selectionModeButton3);
  189.     column3.add(selectionModeButton3);
  190.  
  191.     ActionListener selectionModeButtonListener = new ActionListener() {
  192.         public void actionPerformed(ActionEvent e) {
  193.             JRadioButton source = (JRadioButton)e.getSource();
  194.             boolean flag = source.isSelected();
  195.                 if (flag) {
  196.                     if (source == selectionModeButton1) {
  197.                         tableView.setSelectionMode(0);
  198.                     }
  199.                     else
  200.                     if (source == selectionModeButton2) {
  201.                         tableView.setSelectionMode(1);
  202.                     }
  203.                     else
  204.                     if (source == selectionModeButton3) {
  205.                         tableView.setSelectionMode(2);
  206.                     }
  207.                 }
  208.                 tableView.repaint();
  209.         }
  210.         };
  211.  
  212.         selectionModeButton1.addActionListener(selectionModeButtonListener);
  213.         selectionModeButton2.addActionListener(selectionModeButtonListener);
  214.         selectionModeButton3.addActionListener(selectionModeButtonListener);
  215.  
  216.         controlPanel.add(column3);
  217.  
  218.         // Radio buttons for column resize mode.
  219.  
  220.     column4.setBorder(new TitledBorder("Autoresize"));
  221.         column4.setLayout(new ColumnLayout());
  222.  
  223.     ButtonGroup resizeModeGroup = new ButtonGroup();
  224.  
  225.     final JRadioButton resizeModeButton1 = new JRadioButton("Off", false);
  226.     resizeModeGroup.add(resizeModeButton1);
  227.     column4.add(resizeModeButton1);
  228.  
  229.     final JRadioButton resizeModeButton2 = new JRadioButton("Last column", false);
  230.     resizeModeGroup.add(resizeModeButton2);
  231.     column4.add(resizeModeButton2);
  232.  
  233.     final JRadioButton resizeModeButton3 = new JRadioButton("All columns", true);
  234.     resizeModeGroup.add(resizeModeButton3);
  235.     column4.add(resizeModeButton3);
  236.  
  237.     ActionListener resizeModeButtonListener = new ActionListener() {
  238.         public void actionPerformed(ActionEvent e) {
  239.             JRadioButton source = (JRadioButton)e.getSource();
  240.             boolean flag = source.isSelected();
  241.                 if (flag) {
  242.                     if (source == resizeModeButton1) {
  243.                         tableView.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
  244.                     }
  245.                     else
  246.                     if (source == resizeModeButton2) {
  247.                         tableView.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
  248.                     }
  249.                     else
  250.                     if (source == resizeModeButton3) {
  251.                         tableView.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
  252.                     }
  253.                 }
  254.         }
  255.         };
  256.  
  257.  
  258.         resizeModeButton1.addActionListener(resizeModeButtonListener);
  259.         resizeModeButton2.addActionListener(resizeModeButtonListener);
  260.         resizeModeButton3.addActionListener(resizeModeButtonListener);
  261.         controlPanel.add(column4);
  262.  
  263.         // Create the table.
  264.         tableAggregate = createTable();
  265.         mainPanel.add(tableAggregate, BorderLayout.CENTER);
  266.     }
  267.  
  268.  
  269.     private ImageIcon loadIcon(String name, String description) {
  270.     String path = "images/ImageClub/food/" + name;
  271.     return SwingSet.sharedInstance().loadImageIcon(path, description);
  272.     }
  273.  
  274.     public JScrollPane createTable() {
  275.  
  276.         // final
  277.         final String[] names = {"First Name", "Last Name", "Favorite Color",
  278.                                 "Favorite Sport", "Favorite Number", "Favorite Food"};
  279.  
  280.     ImageIcon burger = loadIcon("burger.gif","burger");
  281.     ImageIcon fries = loadIcon("fries.gif","fries");
  282.     ImageIcon softdrink = loadIcon("softdrink.gif","soft drink");
  283.     ImageIcon hotdog = loadIcon("hotdog.gif","hot dog");
  284.     ImageIcon pizza = loadIcon("pizza.gif","pizza");
  285.     ImageIcon icecream = loadIcon("icecream.gif","ice cream");
  286.     ImageIcon pie = loadIcon("pie.gif","pie");
  287.     ImageIcon cake = loadIcon("cake.gif","cake");
  288.     ImageIcon donut = loadIcon("donut.gif","donut");
  289.     ImageIcon treat = loadIcon("treat.gif","treat");
  290.     ImageIcon grapes = loadIcon("grapes.gif","grapes");
  291.     ImageIcon banana = loadIcon("banana.gif","banana");
  292.     ImageIcon watermelon = loadIcon("watermelon.gif","watermelon");
  293.     ImageIcon cantaloupe = loadIcon("cantaloupe.gif","cantaloupe");
  294.     ImageIcon peach = loadIcon("peach.gif","peach");
  295.     ImageIcon broccoli = loadIcon("broccoli.gif","broccoli");
  296.     ImageIcon carrot = loadIcon("carrot.gif","carrot");
  297.     ImageIcon peas = loadIcon("peas.gif","peas");
  298.     ImageIcon corn = loadIcon("corn.gif","corn");
  299.     ImageIcon radish = loadIcon("radish.gif","radish");
  300.  
  301.  
  302.         // Create the dummy data (a few rows of names)
  303.         final Object[][] data = {
  304.       {"Mike", "Albers",        Color.green, "Soccer", new Integer(44), banana},
  305.       {"Mark", "Andrews",       Color.red, "Baseball", new Integer(2), broccoli},
  306.       {"Tom", "Ball",           Color.blue, "Football", new Integer(99), burger},
  307.       {"Alan", "Chung",         Color.green, "Baseball", new Integer(838), cake},
  308.       {"Jeff", "Dinkins",       Color.magenta, "Football", new Integer(8), cantaloupe},
  309.       {"Amy", "Fowler",         Color.yellow, "Hockey", new Integer(3), carrot},
  310.       {"Brian", "Gerhold",      Color.green, "Rugby", new Integer(7), corn},
  311.       {"James", "Gosling",      Color.pink, "Tennis", new Integer(21), donut},
  312.       {"David", "Karlton",      Color.red, "Baseball", new Integer(1), fries},
  313.       {"Dave", "Kloba",         Color.yellow, "Football", new Integer(14), grapes},
  314.       {"Peter", "Korn",         new Color(100, 100, 255), "Scuba Diving", new Integer(12), broccoli},
  315.       {"Dana", "Miller",        Color.blue, "Ice Skating", new Integer(8), banana},
  316.       {"Phil", "Milne",         Color.magenta, "Rugby", new Integer(3), banana},
  317.       {"Dave", "Moore",         Color.green, "Tennis", new Integer(88), peach},
  318.       {"Hans", "Muller",        Color.magenta, "Baseball", new Integer(5), peas},
  319.       {"Rick", "Levenson",      Color.blue, "Football", new Integer(2), pie},
  320.       {"Tim", "Prinzing",       Color.blue, "Baseball", new Integer(22), pizza},
  321.       {"Chester", "Rose",       Color.black, "Hockey", new Integer(0), radish},
  322.       {"Chris", "Ryan",         Color.black, "None", new Integer(6), softdrink},
  323.       {"Ray", "Ryan",           Color.gray, "Football", new Integer(77), treat},
  324.       {"Georges", "Saab",       Color.red, "Hockey", new Integer(4), watermelon},
  325.       {"Tom", "Santos",         Color.blue, "Football", new Integer(3), banana},
  326.       {"Rich", "Schiavi",       Color.blue, "Hockey", new Integer(4), grapes},
  327.       {"Nancy", "Schorr",       Color.blue, "Hockey", new Integer(8), corn},
  328.       {"Violet", "Scott",       Color.magenta, "Basketball", new Integer(44), grapes},
  329.       {"Joseph", "Scheuhammer", Color.green, "Hockey", new Integer(66), corn},
  330.       {"Jeff", "Shapiro",       Color.black, "Skiing", new Integer(42), peach},
  331.       {"Willie", "Walker",      Color.blue, "Hockey", new Integer(4), banana},
  332.       {"Kathy", "Walrath",      Color.blue, "Baseball", new Integer(8), banana},
  333.       {"Arnaud", "Weber",       Color.green, "Football", new Integer(993), peach},
  334.       {"Steve", "Wilson",       Color.green, "Baseball", new Integer(7), fries}
  335.         };
  336.  
  337.         // Create a model of the data.
  338.         TableModel dataModel = new AbstractTableModel() {
  339.             public int getColumnCount() { return names.length; }
  340.             public int getRowCount() { return data.length;}
  341.             public Object getValueAt(int row, int col) {return data[row][col];}
  342.             public String getColumnName(int column) {return names[column];}
  343.             public Class getColumnClass(int c) {return getValueAt(0, c).getClass();}
  344.             public boolean isCellEditable(int row, int col) {return getColumnClass(col) == String.class;}
  345.             public void setValueAt(Object aValue, int row, int column) { data[row][column] = aValue; }
  346.          };
  347.  
  348.  
  349.         // Create the table
  350.         tableView = new JTable(dataModel);
  351.  
  352.         // Show colors by rendering them in their own color.
  353.         DefaultTableCellRenderer colorRenderer = new DefaultTableCellRenderer() {
  354.         public void setValue(Object value) {
  355.             if (value instanceof Color) {
  356.                 Color c = (Color)value;
  357.                 setForeground(c);
  358.                 setText(c.getRed() + ", " + c.getGreen() + ", " + c.getBlue());
  359.             }
  360.         }
  361.  
  362.         };
  363.  
  364.         colorRenderer.setHorizontalAlignment(JLabel.RIGHT);
  365.         tableView.getColumn("Favorite Color").setCellRenderer(colorRenderer);
  366.  
  367.         tableView.setRowHeight(20);
  368.  
  369.         scrollpane = new JScrollPane(tableView);
  370.         return scrollpane;
  371.     }
  372. }
  373.  
  374. class ColumnLayout implements LayoutManager {
  375.  
  376.   int xInset = 5;
  377.   int yInset = 5;
  378.   int yGap = 2;
  379.  
  380.   public void addLayoutComponent(String s, Component c) {}
  381.  
  382.   public void layoutContainer(Container c) {
  383.       Insets insets = c.getInsets();
  384.       int height = yInset + insets.top;
  385.  
  386.       Component[] children = c.getComponents();
  387.       Dimension compSize = null;
  388.       for (int i = 0; i < children.length; i++) {
  389.       compSize = children[i].getPreferredSize();
  390.       children[i].setSize(compSize.width, compSize.height);
  391.       children[i].setLocation( xInset + insets.left, height);
  392.       height += compSize.height + yGap;
  393.       }
  394.  
  395.   }
  396.  
  397.   public Dimension minimumLayoutSize(Container c) {
  398.       Insets insets = c.getInsets();
  399.       int height = yInset + insets.top;
  400.       int width = 0 + insets.left + insets.right;
  401.  
  402.       Component[] children = c.getComponents();
  403.       Dimension compSize = null;
  404.       for (int i = 0; i < children.length; i++) {
  405.       compSize = children[i].getPreferredSize();
  406.       height += compSize.height + yGap;
  407.       width = Math.max(width, compSize.width + insets.left + insets.right + xInset*2);
  408.       }
  409.       height += insets.bottom;
  410.       return new Dimension( width, height);
  411.   }
  412.  
  413.   public Dimension preferredLayoutSize(Container c) {
  414.       return minimumLayoutSize(c);
  415.   }
  416.  
  417.   public void removeLayoutComponent(Component c) {}
  418.  
  419. }
  420.